package logic

import (
	"context"
	"errors"

	"go-weixin/app/guitar/model"
	"go-weixin/app/guitar/rpc/internal/svc"
	"go-weixin/app/guitar/rpc/pb/proto/guitar"

	"github.com/zeromicro/go-zero/core/logx"
)

type GuitarImageAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGuitarImageAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GuitarImageAddLogic {
	return &GuitarImageAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GuitarImageAddLogic) GuitarImageAdd(in *guitar.GuitarImageAddReq) (*guitar.GuitarImageAddResp, error) {
	// 查询是否存在
	has, err := l.svcCtx.ChenDb.Exist(&model.GuitarModel{Id: in.GuitarId})
	if err != nil {
		return nil, errors.New("guitar.image.add.mysql查询异常")
	}
	if !has {
		return nil, errors.New("guitar.image.add.不存在此guitar_id")
	}
	// 查询是否存在
	has, err = l.svcCtx.ChenDb.Exist(&model.GuitarImageModel{GuitarId: in.GuitarId, Url: in.Url})
	if err != nil {
		return nil, err
	}
	if has {
		return nil, errors.New("guitar.image.add此图片已存在")
	}
	// 插入
	accected, err := l.svcCtx.ChenDb.Insert(&model.GuitarImageModel{GuitarId: in.GuitarId, Url: in.Url})
	if err != nil {
		return nil, errors.New("guitar.image.add.mysql.insert.error")
	}
	if accected == 0 {
		return nil, errors.New("guitar.image.add.mysql.insert行数为0")
	}

	return &guitar.GuitarImageAddResp{}, nil
}
